/*
package com.example.wordlistdemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.wordlistdemo.dao.DatabaseHelper;
import com.example.wordlistdemo.util.DateUtils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExportActivity extends AppCompatActivity {

    private List<WordEntity> wordList;

    private DatabaseHelper dbHelper;

    private static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;

    private boolean permissionGranted = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();
        dbHelper = new DatabaseHelper(this);
        wordList = dbHelper.listAll();
        // 初始化 wordList（假设你已经有了数据）
        // wordList = ...

        // 其他初始化操作
    }

    private void checkPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            // Permission is not granted, request it
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
        } else {
            // Permission is already granted
            permissionGranted = true;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.export_menu_item) {
            if (permissionGranted) {
                exportToExcel();
            } else {
                // Permission is not granted, request it
                checkPermission();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void exportToExcel() {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("WordList");

        // 添加表头
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("English");
        headerRow.createCell(1).setCellValue("Chinese");
        headerRow.createCell(2).setCellValue("Pronunciation");
        headerRow.createCell(3).setCellValue("Familiar");

        // 添加数据
        for (int i = 0; i < wordList.size(); i++) {
            Row row = sheet.createRow(i + 1);
            WordEntity word = wordList.get(i);
            row.createCell(0).setCellValue(word.getEnglish());
            row.createCell(1).setCellValue(word.getChinese());
            row.createCell(2).setCellValue(word.getPhonetic());
            row.createCell(3).setCellValue(word.isFamiliar() ? "1" : "0");
        }

        // 保存Excel文件到Download/aa目录
        try (FileOutputStream fileOut = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/aaa/word_list3"+ DateUtils.getCurrentTime() +".xls")) {
            workbook.write(fileOut);
            fileOut.close();
            Toast.makeText(this, "Exported to Excel successfully", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "Export failed", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission granted, set the flag to true
                permissionGranted = true;
                // Execute the operation that requires the permission
                exportToExcel();
            } else {
                // Permission denied, you may inform the user or handle it accordingly
                Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

*/
